package common.Location;

import globals.GlobalObjects;
import graphics.GraphicalObject;
import graphics.Shapes.Sphere;

import java.util.ArrayList;
import java.util.HashMap;

import org.eclipse.swt.graphics.ImageData;

import common.IParse;
import common.Position3D;


public class LocationsMap implements IParse{
	private class Location {
		public int index;
		public Position3D position;
	}

	String mapFileName = null; 
	ImageData mapImage = null;

	String sphereName = null;
	Sphere sphere;
	
	ArrayList<Location> locations = new ArrayList<Location>();
	HashMap<Integer, String> indexNameMap = new HashMap<Integer, String>();
	private String name;
	
	public void commit(){
		mapImage = new ImageData(mapFileName);
		sphere = (Sphere)GlobalObjects.getGraphic(sphereName);
		readMap();
		addObjectsToSphere();
	}
	private void readMap() {
		for (int x = 0; x < mapImage.width; x++){
			for (int y = 0; y < mapImage.height; y++){
				createLocation(mapImage.getPixel(x, y), 
						(double)x/ (mapImage.width-1),
						(double)y/(mapImage.height-1));
			}
		}
	}
	
	private void createLocation(int data, double x, double y) {
		int index = data & 0xff;
		if (index == 0 || index == 255 )
			return;
		
		Position3D position = new Position3D();
		position.getCenter().y += 1;
		position.rotate(0, 0 , -Math.PI * y);
		position.rotate(0, -Math.PI * 2 * x, 0);

		Location location = new Location();
		location.index = index;
		location.position = position;
		locations.add(location);
	}

	private void addObjectsToSphere(){
		
		ArrayList<GraphicalObject> joinPoints = new ArrayList<GraphicalObject>();
		
		for (Location location : locations){

			// find graphical object
			int index = location.index;
			Position3D position = location.position;
			String name = indexNameMap.get(index);
			if (name == null){
				//System.out.println("[locationmap] invalid index (" + index + ")" );
				continue;
			}
			GraphicalObject gobject = GlobalObjects.getGraphic(name);
			if (gobject == null){
				//System.out.println("[locationmap] invalid name (" + name + ")" );
				continue;
			}
			
			// create join point
			GraphicalObject joinPoint = new GraphicalObject();
			joinPoint.setPosition(position);
			joinPoint.addObject(gobject);
			joinPoints.add(joinPoint);
		}
		attachJointPoints(joinPoints);
	}
	
	private void attachJointPoints(ArrayList<GraphicalObject> joinPoints){
		for (GraphicalObject joinpoint : joinPoints )
		{
			// set position to sphere surface
			joinpoint.getCenter().scale(sphere.surfaceHeight(joinpoint.getCenter()));
			// attach join point
			sphere.addObject(joinpoint);
		}
	}
	
	public void setParameter(String name, String[] params) {
		if (name.equals("filename")) {
			mapFileName = params[0];	
		}
		if (name.equals("index")) {
			int index = Integer.parseInt(params[0]);	
			String objectName = params[1];	
			indexNameMap.put(index, objectName);
		}
		if (name.equals("sphere")) {
			sphereName = params[0];
		}
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

}

